//
//  XTStatusOriginalView.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/17.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

class XTStatusOriginalView: UIView {

    //MARK: - 属性
    var status : XTStatus?{
    
        didSet {
            
            //给子控件赋值
            
            //头像
            if let strURL = status?.user?.profile_image_url {
                
                let url = NSURL(string: strURL)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            //用户名称
            let userName = status?.user?.screen_name
            nameLabel.text = userName
            
            //vip
            let vipLevel = status?.user?.mbrank ?? 0
            
            if vipLevel > 0 && vipLevel <= 6 {
            
                vipImageView.image = UIImage(named: "common_icon_membership_level\(vipLevel)")
            }else if vipLevel == 0 {
                
                vipImageView.image = nil
            }else {
                vipImageView.image = nil
            }
            
            //微博创建时间
            let time = NSDate.date((status?.created_at)!).sinaDateDscription()
            createdTimeLabel.text = time
            
            //微博来源
            sourceLabel.text = status?.source
            
            //认证
            let verifiedType = status?.user?.verified_type ?? -1
            
            switch verifiedType {
                
            case 0:
                verifiedTypeImageView.image = UIImage(named: "avatar_vip")
            case 2,3,5:
                verifiedTypeImageView.image = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verifiedTypeImageView.image = UIImage(named: "avatar_grassroot")
            default:
                verifiedTypeImageView.image = nil
            }
            
            //微博内容
            textField.text = status?.text
            
            
            //给pictureView属性赋值
            pictrueView.pictureURLs = status?.pictureURLs
            //更新pictrueView的约束
            pictrueView.snp_updateConstraints { (make) -> Void in
                let margin = status?.pictureURLs?.count == 0 ? 0 : cellMargin
                make.top.equalTo(textField.snp_bottom).offset(margin)
            }
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//
        
        //设置子控件
        prepareUI()
    }

    
    //设置子控件
    private func prepareUI() {
    
        //设置背景颜色
        self.backgroundColor = UIColor.whiteColor()
        
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(createdTimeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedTypeImageView)
        addSubview(textField)
        addSubview(pictrueView)
        
        //设置约束
        
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(self).offset(cellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        //用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(cellMargin)
            make.top.equalTo(iconView)
        }
        
        //vip
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
            make.top.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        //微博时间
        createdTimeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(createdTimeLabel.snp_trailing).offset(cellMargin)
            make.centerY.equalTo(createdTimeLabel)
        }
        
        //认证
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-2.5)
            make.centerY.equalTo(iconView.snp_bottom).offset(-2.5)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        //微博内容
        textField.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(verifiedTypeImageView.snp_bottom)
            
        }
        
        //pictrueView
        pictrueView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(textField.snp_bottom).offset(cellMargin)
            make.trailing.equalTo(self).offset(-cellMargin)
            //测试
//            make.height.equalTo(359)
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictrueView).offset(cellMargin)
            
        }
        
    }
    
    
    
    
    
    
    
    //MARK: - 懒加载
    
    //头像图标
    private lazy var iconView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar"))
        
        //设置圆角
        imageView.layer.cornerRadius = 17.5
        
        return imageView
    }()
    
    //用户名称
    private lazy var nameLabel : UILabel = {
        let label = UILabel()
        
        //设置
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(NameFontSize)
        label.text = "小熊"
        
        return label
    }()
    
    //vip
    private var vipImageView : UIImageView = UIImageView(image: nil)
    
    //发微博时间
    private lazy var createdTimeLabel : UILabel = {
        let label = UILabel()
        
        //设置
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        label.text = "时间"
        
        return label
        
    }()
    
    //来源
    private var sourceLabel : UILabel = {
        let label = UILabel()
        
        //设置
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(TimeFontSize)
        label.text = "来源"
        
        return label
    
    }()
    
    //认证
    private var verifiedTypeImageView : UIImageView = UIImageView(image: nil)
    
    //文本内容
    
    private var textField : UILabel = {
        let label = UILabel()
        
        //设置
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - cellMargin * 2
        
        return label
        
    }()
    
    //pictrueView
    private lazy var pictrueView : XTStatusPictrueView = XTStatusPictrueView()
    
}


